<?php
class ImagesAction extends Action {
    /*public function _initialize(){	
		
    }*/
	#列表
	public function index(){
		//var_dump(function_exists('curl_init'));exit;
		$query_option = array(
			"q" => '手机风景',
			"num" => 10,
			"hl" => 'zh-CN',
			"newwindow" => '1',
			"safe" => 'strict',
			"site" => 'imghp',
			"biw" => 1440,//窗口宽度
			"bih" => 900,//窗口高度
			"tbm" => 'isch',
			"ijn" => 0,
			"ei" => '3iztT8a9GOO4iQeHhfmxDQ',
			"page" => 1,
			"start" => 1,
			"tbs"=>"isz:ex,iszw:640,iszh:960",//大小筛选
		);
		$query_option['ijn'] = $_REQUEST['p']?intval($_REQUEST['p'])-1:0;
		foreach($query_option as $key=>&$q){
			if(isset($_REQUEST[$key])){
				$q = $_REQUEST[$key];
			}
		}
		
		$query = http_build_query($query_option);
		import("@.ORG.cURL");
		$cURL = new cURL();
		$html = $cURL->get('http://www.google.com.hk/search?'.$query);
		preg_match_all('/<li .*><a href="\\/imgres\\?(.*)".*><img .*src="(.*)".*height=(\\d+) width=(\\d+) /U',$html,$reg);
		$data = array();
		//print_r($html);exit;
		foreach($reg[2] as $key=>$vo){
			$reg[1][$key] = str_replace('amp;','',$reg[1][$key]);
			parse_str($reg[1][$key], $vars); 
			$data[$key] = $vars;
			$data[$key]['thumb']=$vo;
			$data[$key]['thumb_h']=$reg[3][$key];
			$data[$key]['thumb_w']=$reg[4][$key];
		}

		$this->assign('list',$data);
		//print_r($data);exit;
		if(!empty($query_option['ijn'])){$this->display('item');exit;}
		$this->assign('query_option',$query_option);
		$path_arr = array(
			'q'=>$query_option['q'],
			'biw'=>$query_option['biw'],
			'bih'=>$query_option['bih'],
			'ijn'=>($query_option['ijn'])
			);
		$this->assign('path_arr',$path_arr);
		$this->display();
	}
	public function display($templateFile = NULL){
		//echo $_SERVER['REQUEST_URI'];exit;
    	$pathinfo = pathinfo($_SERVER['REQUEST_URI']);
    	//$retType = $pathinfo['extension'] == 'json' ? 'json' : 'xml';
		if($pathinfo['extension'] != 'html'){
			parent::display();
		}
        if(APP_DEBUG){
            parent::display();
        }else{
            echo parent::buildHtml($pathinfo['filename'],'.'.$pathinfo['dirname'].'/',$templateFile);
        }
        exit;
    
	}
	
}